package com.baizhi.serviceimpl;

import com.baizhi.annotation.AddCache;
import com.baizhi.annotation.AddLog;
import com.baizhi.annotation.DelCache;
import com.baizhi.dao.LikeMapper;
import com.baizhi.dao.VideoMapper;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.LikeExample;
import com.baizhi.entity.Video;
import com.baizhi.entity.VideoExample;
import com.baizhi.po.VideoPo;
import com.baizhi.service.VideoService;
import com.baizhi.util.ALiYunUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class VideoServiceImpl implements VideoService {
    @Resource
    VideoMapper videoMapper;
    @Resource
    HttpServletRequest request;
    @Resource
    LikeMapper likeMapper;
    @AddCache
    @Override
    public HashMap<String, Object> queryAllPage( PageDTO pageDTO) {
        HashMap<String, Object> map = new HashMap<>();
        //设置当前页
        map.put("page",pageDTO.getPage());
        //一页的条数
        map.put("pageSize",pageDTO.getPageSize());
        //设置条件查询对象
        VideoExample example = new VideoExample();
        //查询总条数
        int count = videoMapper.selectCountByExample(example);
        //设置总条数
        map.put("total",count);
        //创建分页对象   起始条数  数据数
        RowBounds rowBounds = new RowBounds((pageDTO.getPage()-1)*pageDTO.getPageSize(),pageDTO.getPageSize());
        List<Video> videos = videoMapper.selectByExampleAndRowBounds(example, rowBounds);
        //设置数据
        map.put("rows",videos);
        return map;
    }
    @DelCache
    @AddLog(value = "修改视频")
    @Override
    public HashMap<String, Object> update(Video video) {
        HashMap<String, Object> map = new HashMap<>();

        try {
            videoMapper.updateByPrimaryKeySelective(video);
            map.put("message","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","修改失败");
        }
        return map;
    }

    @Override
    public Video queryById(String id) {

        return videoMapper.selectByPrimaryKey(id);
    }
    @DelCache
    @AddLog(value = "添加视频")
    @Override
    public HashMap<String, Object> add(Video video) {
        HashMap<String, Object> map = new HashMap<>();
        video.setId(UUID.randomUUID().toString());
        try {
            videoMapper.insertSelective(video);
            map.put("message","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","添加失败");
        }
        return map;
    }
    @DelCache
    @AddLog(value = "删除视频")
    @Override
    public void delete(Video video) {
        //根据文件信息
        Video videos = videoMapper.selectOne(video);
        System.out.println(videos);
        //获取图片网络路径
        String videoPath = videos.getVideoPath();
        String coverPath = videos.getCoverPath();
        System.out.println(videoPath+"aaaaaa");
        System.out.println(coverPath+"======");
        //字符串处理
        String videoPathName = videoPath.replace("https://yingx-2013.oss-cn-beijing.aliyuncs.com/", "");
        String coverPathName = coverPath.replace("https://yingx-2013.oss-cn-beijing.aliyuncs.com/", "");
        //阿里云删除文件和截图
        ALiYunUtil.deleteBucket("yingx-2013",videoPathName);
        ALiYunUtil.deleteBucket("yingx-2013",coverPathName);
        //删除数据
        videoMapper.delete(video);
    }

    @Override
    public HashMap<String, String> uploadVideoALiY(MultipartFile videoFile) {
        //获取文件名
        String filename = videoFile.getOriginalFilename();

        //文件名拼接唯一标识
        String newName=new Date().getTime()+"-"+filename;
        //文件名拼接目录名
        String objectName="video/"+newName;


        ALiYunUtil.uploadFileBytes("yingx-2013",objectName,videoFile);

        URL url = ALiYunUtil.videoInterceptCover("yingx-2013", objectName);
        System.out.println(url);

        //通过视频名获取图片名
        String coverName="videoCover/"+newName.split("\\.")[0]+".jpg";

        try {
            ALiYunUtil.uploadNetIO("yingx-2013",coverName,url.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }


        //拼接文件的网络地址
        String netPath="https://yingx-2013.oss-cn-beijing.aliyuncs.com/"+objectName;
        String coverNames="https://yingx-2013.oss-cn-beijing.aliyuncs.com/"+coverName;

        HashMap<String, String> map = new HashMap<>();

        map.put("fileName",netPath);
        map.put("coverName",coverNames);

        return map;
    }
    @Override
    public List<VideoPo> queryByReleaseTime() {
        //获取数据
        List<VideoPo> videoPos = videoMapper.queryByReleaseTime();
        //遍历视频的id
        for (VideoPo videoPo : videoPos) {
            //获取视频id
            String videoId = videoPo.getId();

            LikeExample example = new LikeExample();
            example.createCriteria().andVideoIdEqualTo(videoId);
            //根据视频的id查询点赞数
            Integer count = likeMapper.selectCountByExample(example);
            //将点赞数放入对象
            videoPo.setLikeCount(count);
        }
        return videoPos;
    }

}
